package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.AudioFrequencyBrowseRecordPresenter
import com.weetop.cfw.base.view.AudioFrequencyBrowseRecordView
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.activity.AudioFrequencyBrowseRecordActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class AudioFrequencyBrowseRecordPresenterImp(var audioFrequencyBrowseRecordView: AudioFrequencyBrowseRecordView? = null): AudioFrequencyBrowseRecordPresenter {

    /**
     * 获取音视频浏览记录
     * @param curpage 当前数据的页数
     */
    override fun getAudioBrowsingRecord(audioFrequencyBrowseRecordActivity: AudioFrequencyBrowseRecordActivity, curpage: Int) {
        audioFrequencyBrowseRecordActivity.showLoadingDialog(audioFrequencyBrowseRecordActivity,"正在加载云课程浏览记录...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getAudioBrowsingRecord(curpage = curpage),audioFrequencyBrowseRecordActivity,object:RxJavaCallBack<AudioVideoBrowsingRecordBean>() {
            override fun onSuccess(t: AudioVideoBrowsingRecordBean?) {
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
                audioFrequencyBrowseRecordView?.getAudioBrowsingRecordSuccess(t)
            }

            override fun onRequestError(t: AudioVideoBrowsingRecordBean?) {
                super.onRequestError(t)
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
                audioFrequencyBrowseRecordView?.getAudioBrowsingRecordFailed()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
                audioFrequencyBrowseRecordView?.getAudioBrowsingRecordFailed()
            }

    })}

    /**
     * 清空音视频浏览记录
     */
    override fun clearAudioBrowsingRecord(audioFrequencyBrowseRecordActivity: AudioFrequencyBrowseRecordActivity) {
        audioFrequencyBrowseRecordActivity.showLoadingDialog(audioFrequencyBrowseRecordActivity,"正在清空管理音频浏览记录...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().clearBrowseRecordList(3),audioFrequencyBrowseRecordActivity,object:RxJavaCallBack<ErrorBean>(){
            override fun onSuccess(t: ErrorBean?) {
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
                audioFrequencyBrowseRecordView?.showNativeShortToast("管理音频浏览记录清除成功")
                audioFrequencyBrowseRecordView?.clearAudioFrequencyBrowseRecordSuccess()
            }

            override fun onRequestError(t: ErrorBean?) {
                super.onRequestError(t)
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
                audioFrequencyBrowseRecordView?.showNativeShortToast(t?.message)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                audioFrequencyBrowseRecordActivity.dismissLoadingDialog()
            }

        })
    }

    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        audioFrequencyBrowseRecordView = baseView as? AudioFrequencyBrowseRecordView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        audioFrequencyBrowseRecordView = null
    }
}